package com.wangjiacai.utils.cos.impl;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.*;
import com.wangjiacai.utils.cos.CosRepository;
import com.wangjiacai.utils.cos.ObjectService;

import java.io.File;
import java.io.InputStream;
import java.util.List;

public class ObjectServiceImpl extends CosRepository implements ObjectService {

    private String bucketName;

    public ObjectServiceImpl(String secretId, String secretKey, String region) {
        super(secretId, secretKey, region);
    }

    public ObjectServiceImpl(String secretId, String secretKey, String region, String bucketName) {
        super(secretId, secretKey, region);
        this.bucketName = bucketName;
    }

    @Override
    public PutObjectResult putObject(String bucketName, String key, File file) {
        COSClient cosClient = getCosClient();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        return putObjectResult;
    }

    @Override
    public PutObjectResult putObject(String bucketName, String key, InputStream inputStream) {
        COSClient cosClient = getCosClient();
        ObjectMetadata objectMetadata = new ObjectMetadata();
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, objectMetadata);
        PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
        return putObjectResult;
    }

    @Override
    public void deleteObject(String bucketName, String key) {
        COSClient cosClient = getCosClient();
        cosClient.deleteObject(bucketName, key);
    }

    @Override
    public PutObjectResult putObject(String key, File file) {
        return putObject(bucketName, key, file);
    }

    @Override
    public PutObjectResult putObject(String key, InputStream input) {
        return putObject(bucketName, key, input);
    }

    @Override
    public void deleteObject(String key) {
        deleteObject(bucketName, key);
    }

    @Override
    public String getDefaultBucketName() {
        return bucketName;
    }

    @Override
    public void emptyBucket(String bucketName) {
        COSClient cosClient = getCosClient();
        ObjectListing objectListing = cosClient.listObjects(bucketName);
        List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
        cosObjectSummaries.stream().forEach(cosObjectSummary -> {
            cosClient.deleteObject(cosObjectSummary.getBucketName(), cosObjectSummary.getKey());
        });
    }

    @Override
    public void emptyBucket() {
        emptyBucket(bucketName);
    }

    @Override
    public void emptyBucketFilterSuffix(String suffix) {
        emptyBucketFilterSuffix(bucketName, suffix);
    }

    @Override
    public void emptyBucketFilterSuffix(String bucketName, String suffix) {
        COSClient cosClient = getCosClient();
        ObjectListing objectListing = cosClient.listObjects(bucketName);
        List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
        cosObjectSummaries.stream().filter(cosObjectSummary -> cosObjectSummary.getKey().endsWith(suffix)).forEach(cosObjectSummary -> {
            cosClient.deleteObject(cosObjectSummary.getBucketName(), cosObjectSummary.getKey());
        });
    }
}
